package com.example.cxx.controller;

import com.example.cxx.mapper.ScoreSubMapper;
import com.example.cxx.mapper.UserMapper;
import com.example.cxx.pojo.Result;
import com.example.cxx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("course/api/sub/*")
public class ScoreSubController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ScoreSubMapper scoreSubMapper;
    private String openid;
    @GetMapping("/getScore")
    public Result scoreSub(Integer userAccount,String userPass){
        if(userAccount==null||userPass==null){
            return Result.error("error");
        }
        User user=userMapper.selectUser(userAccount,userPass);
        if(user!=null){

            try {
                openid=userMapper.selectOpenid(user.getId());
            }catch (Exception e){
                userMapper.deleteDes("missing co");
                openid=userMapper.selectOpenid(user.getId());
            }
            if(scoreSubMapper.exist(openid)){
                if(scoreSubMapper.selectSubStatus(openid)==0){
                    return Result.error("no");
                }
                else {
                    return Result.error("yes");
                }

            }else {
                scoreSubMapper.insertScore(openid);
                return Result.error("no");
            }
        }else {
            return Result.error("error");
        }

    }
    @GetMapping("/setScore")
    public Result setScoreSub(Integer status){
        scoreSubMapper.setScoreSub(status,openid);
        return Result.success();
    }
}
